import {
    BroadcastRequest,
    BroadcastResponse,
    Controller,
    ControllerNo,
    ControllerOpt,
    DialogControllerOpt
} from "./model/ControllerOpt";
import {ControllerBroadcast} from "./ControllerBroadcast";
import {SerializeBody} from "./model/SerializeBody";
import {FinalConstant, FinalKey} from "../constant/FinalConstant";
import Configer from "../frame/apiModel/config/Configer";
import {PlayerModel} from "../frame/apiModel/adapterModel/model4/PlayerModel";
import {PlayerFactory} from "../frame/PlayerFactory";
import {GlobalModel} from "../frame/apiModel/adapterModel/model1/GlobalModel";

// ---------------------------------------------------------------------------------------------------------------------------------------------------------------
export class DialogController implements Controller {
    getControllerNo(): number {
        return ControllerNo.DialogController;
    }
    static get dialogs(): dialog[] {
        return this._dialogs;
    }
    static get dialogOpts(): Map<button, ControllerOpt> {
        return this._dialogOpts;
    }
    private static dialogController: DialogController = new DialogController();
    // <playerId, dialog> dialog 每人一个
    private static _dialogs: dialog[] = []
    // 根据dialog找dialogId
    private static _dialogOpts: Map<button, ControllerOpt> = new Map<button, ControllerOpt>();
    static getInstance() {
        return DialogController.dialogController;
    }
    init() {
        PlayerFactory.getInstance().mapAllNotEndPlayingPlayer(
            (player: PlayerModel) => {
                const dialog = DialogCreate();
                const id = player.playerId;
                DialogController.dialogs[id] = dialog;
            }
        );
        ControllerBroadcast.getInstance().startListen(DialogController.getInstance());
    }
    loadByJson(json: SerializeBody, player: PlayerModel) {
    }
    getDialogHotKeys = () => {
        const hotKeys = Configer.getFinal(FinalKey.hotKeys) as number[];
        return hotKeys;
    }
    // 每个人只有一个dialog
    showDialog = (param: { player: PlayerModel, dialogName: string, optList: DialogControllerOpt[] }): dialog => {
        const playerId = param.player.playerId;
        const dialog = DialogCreate();
        DialogController.dialogs[playerId] = dialog;
        DialogSetMessage(dialog, param.dialogName);
        const hotKeys = this.getDialogHotKeys();
        for (let i = 0; i < param.optList.length; i++) {
            const opt = param.optList[i];
            const text = opt.id;
            const button = DialogAddButton(dialog, text, hotKeys[i]);
            DialogController.dialogOpts.set(button, opt);
            const trigger = CreateTrigger();
            TriggerRegisterDialogEvent(trigger, dialog);
            TriggerAddAction(trigger, DialogController.buttonClickEvent);
        }
        DialogDisplay(param.player.handle, dialog, true);
        return dialog;
    }
    // 这个方法无法绑定this，使用的变量只能是静态变量
    static buttonClickEvent(this: void) {
        const player = GetTriggerPlayer();
        const button = GetClickedButtonBJ();
        const buttonOpt = DialogController.dialogOpts.get(button);
        if (!buttonOpt) {
            return;
        }
        const callBack = buttonOpt.callBack;
        const self  = buttonOpt.self;
        callBack && callBack(self);
        DialogController.dialogOpts.delete(button);
        // closeDialog
        const playerId = GetPlayerId(player);
        const dialog = DialogController.dialogs[playerId];
        // DialogDestroy(dialog);
    }
    notify(request: BroadcastRequest): BroadcastResponse {
        return undefined;
    }
    serialize(): SerializeBody {
        return {};
    }
}